import pika

'''
暂时不消费，用了原有的消费功能
'''


class RabbitMQConsumer:

    def __init__(self, host, port, username, password, queue_name, vhost):
        credentials = pika.PlainCredentials(username, password)
        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=host, port=port, virtual_host=vhost, credentials=credentials))
        self.channel = self.connection.channel()
        self.channel.queue_declare(queue=queue_name)
        self.queue_name = queue_name
        self.processed_messages = set()

    def consume_messages(self, callback):
        def on_message_callback(ch, method, properties, body):
            message_id = properties.message_id
            if message_id not in self.processed_messages:
                self.processed_messages.add(message_id)
                callback(ch, method, properties, body)

        self.channel.basic_consume(queue=self.queue_name, on_message_callback=on_message_callback, auto_ack=True)
        self.channel.start_consuming()

    def close_connection(self):
        self.connection.close()
